package com.jeegit.ecms.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jeegit.common.annotation.Log;
import com.jeegit.common.enums.BusinessType;
import com.jeegit.ecms.domain.CmsVideoCategory;
import com.jeegit.ecms.service.ICmsVideoCategoryService;
import com.jeegit.ecms.utils.ECMSUtils;
import com.jeegit.common.core.controller.BaseController;
import com.jeegit.common.core.domain.AjaxResult;
import com.jeegit.common.utils.poi.ExcelUtil;
import com.jeegit.common.utils.StringUtils;
import com.jeegit.common.core.domain.Ztree;

/**
 * 视频分类Controller
 * 
 * @author jeegit
 * @date 2022-06-25
 */
@Controller
@RequestMapping("/ecms/videocategory")
public class CmsVideoCategoryController extends BaseController
{
    private String prefix = "ecms/videocategory";

    @Autowired
    private ICmsVideoCategoryService cmsVideoCategoryService;

    @RequiresPermissions("ecms:videocategory:view")
    @GetMapping()
    public String videocategory(CmsVideoCategory cmsVideoCategory,ModelMap mmap)
    {
    	mmap.put("cmsVideoCategory", cmsVideoCategory);
    	mmap.put("cmsSiteList", ECMSUtils.getCmsSiteList());
        return prefix + "/videocategory";
    }

    /**
     * 查询视频分类树列表
     */
    @RequiresPermissions("ecms:videocategory:list")
    @PostMapping("/list")
    @ResponseBody
    public List<CmsVideoCategory> list(CmsVideoCategory cmsVideoCategory)
    {
        List<CmsVideoCategory> list = cmsVideoCategoryService.selectCmsVideoCategoryList(cmsVideoCategory);
        return list;
    }

    /**
     * 导出视频分类列表
     */
    @RequiresPermissions("ecms:videocategory:export")
    @Log(title = "视频分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CmsVideoCategory cmsVideoCategory)
    {
        List<CmsVideoCategory> list = cmsVideoCategoryService.selectCmsVideoCategoryList(cmsVideoCategory);
        ExcelUtil<CmsVideoCategory> util = new ExcelUtil<CmsVideoCategory>(CmsVideoCategory.class);
        return util.exportExcel(list, "视频分类数据");
    }

    /**
     * 新增视频分类
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("cmsVideoCategory", cmsVideoCategoryService.selectCmsVideoCategoryById(id));
        }
        mmap.put("cmsVideoCategory", new CmsVideoCategory());
        mmap.put("cmsSiteList", ECMSUtils.getCmsSiteList());
        return prefix + "/add";
    }

    /**
     * 新增保存视频分类
     */
    @RequiresPermissions("ecms:videocategory:add")
    @Log(title = "视频分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CmsVideoCategory cmsVideoCategory)
    {
        return toAjax(cmsVideoCategoryService.insertCmsVideoCategory(cmsVideoCategory));
    }

    /**
     * 修改视频分类
     */
    @RequiresPermissions("ecms:videocategory:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CmsVideoCategory cmsVideoCategory = cmsVideoCategoryService.selectCmsVideoCategoryById(id);
        //初始化树结构
        ECMSUtils.initECMSTreeData(cmsVideoCategory);
        mmap.put("cmsVideoCategory", cmsVideoCategory);
        mmap.put("cmsSiteList", ECMSUtils.getCmsSiteList());
        return prefix + "/edit";
    }

    /**
     * 修改保存视频分类
     */
    @RequiresPermissions("ecms:videocategory:edit")
    @Log(title = "视频分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CmsVideoCategory cmsVideoCategory)
    {
        return toAjax(cmsVideoCategoryService.updateCmsVideoCategory(cmsVideoCategory));
    }

    /**
     * 删除
     */
    @RequiresPermissions("ecms:videocategory:remove")
    @Log(title = "视频分类", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(cmsVideoCategoryService.deleteCmsVideoCategoryById(id));
    }

    /**
     * 选择视频分类树
     */
    @GetMapping(value = { "/selectVideocategoryTree/{id}", "/selectVideocategoryTree/" })
    public String selectVideocategoryTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("cmsVideoCategory", cmsVideoCategoryService.selectCmsVideoCategoryById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载视频分类树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = cmsVideoCategoryService.selectCmsVideoCategoryTree();
        return ztrees;
    }
}
